package com.erp.utils;

import java.lang.reflect.Field;
import java.util.HashMap;

/**
 * @ClassName MapUtils
 * @Description TODO
 * @Author songjizhen
 * @Date 2020/6/17 17:09
 * @Version 1.0
 */
public class MapUtils {
    /**
     * 对象转map
     * @param obj
     * @return
     * @throws Exception
     */
    public  static HashMap<String,Object> convertToMap(Object obj) throws Exception {
        HashMap<String,Object> map=new HashMap<String, Object>();
        Field[] fields=obj.getClass().getDeclaredFields();
        for(int i=0,len=fields.length;i<len;i++){
            String varName=fields[i].getName();
            boolean accessFlag=fields[i].isAccessible();
            Object o=fields[i].get(obj);
            if(o !=null){
                map.put(varName,o);
            }
            fields[i].setAccessible(accessFlag);
        }

        return map;
    }
}
